package server

func WithName(name string) SetOption {
	return func(s *Option) error {
		s.Name = name
		return nil
	}
}

func WithListen(listen string) SetOption {
	return func(s *Option) error {
		s.Listen = listen
		return nil
	}
}

func WithPort(port int) SetOption {
	return func(s *Option) error {
		s.Port = port
		return nil
	}
}

func WithProtocol(protocol string) SetOption {
	return func(s *Option) error {
		s.Protocol = protocol
		return nil
	}
}

func NewRPCServer(options ...SetOption) (*RPCServer, error) {
	totalOptions := defaultOptions()
	totalOptions = append(totalOptions, withRPCProtocol())
	totalOptions = append(totalOptions, options...)

	rs := &RPCServer{
		option:  &Option{},
		gServer: nil,
	}

	for _, op := range totalOptions {
		if err := op(rs.option); err != nil {
			return nil, err
		}
	}
	if err := rs.initRpcServer(); err != nil {
		return nil, err
	}
	return rs, nil
}
